<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单id" prop="ordersId">
        <el-input
          v-model="queryParams.ordersId"
          placeholder="请输入订单id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品id" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="捶成状态" prop="beatInto">
        <el-input
          v-model="queryParams.beatInto"
          placeholder="1=捶成 0=失败 2=进行中"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取状态" prop="receive">
        <el-input
          v-model="queryParams.receive"
          placeholder="1=领取 0=未领取"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:beat:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:beat:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:beat:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:beat:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="beatList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="订单id" align="center" prop="ordersId" />
      <el-table-column label="商品id" align="center" prop="goodsId" />
      <el-table-column label="商品名" align="center" prop="goodsName" />
      <el-table-column label="商品主图" align="center" prop="goodsImg">
        <template slot-scope="scope" v-if="scope.row.goodsImg!=null">
          <el-image :preview-src-list="scope.row.goodsImg.split(',')" style="width: 30px;height: 30px;" shape="square"
                    :src="image" :key="image" v-for="image in scope.row.goodsImg.split(',')"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="锤数" align="center" prop="beatSum" />
      <el-table-column label="锤成状态" align="center" prop="beatInto" >
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.beatInto == '0'"
          >失败</el-tag
          >
          <el-tag type="success" v-if="scope.row.beatInto == '1'"
          >成功</el-tag
          >
          <el-tag type="info" v-if="scope.row.beatInto == '2'"
          >进行中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="领取状态" align="center" prop="receive">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.receive == '1'"
            >领取</el-tag
            >
            <el-tag type="warning" v-if="scope.row.receive == '0'"
            >未领取</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过期时间" align="center" prop="beatOutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beatOutTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领取时间" align="center" prop="receiveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiveTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:beat:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:beat:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户捶一捶记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="订单id" prop="ordersId">
          <el-input v-model="form.ordersId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品名" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名" />
        </el-form-item>
        <el-form-item label="商品主图" prop="goodsImg">
          <el-input v-model="form.goodsImg" placeholder="请输入商品主图" />
        </el-form-item>
        <el-form-item label="需要捶的次数" prop="beatSum">
          <el-input v-model="form.beatSum" placeholder="请输入需要捶的次数" />
        </el-form-item>
        <el-form-item label="是否捶成 1=捶成 0=未捶成 2=进行中" prop="beatInto">
          <el-input v-model="form.beatInto" placeholder="请输入是否捶成 1=捶成 0=未捶成 2=进行中" />
        </el-form-item>
        <el-form-item label="是否领取 1=领取 0=未领取" prop="receive">
          <el-input v-model="form.receive" placeholder="请输入是否领取 1=领取 0=未领取" />
        </el-form-item>
        <el-form-item label="捶一捶过期时间" prop="beatOutTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.beatOutTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择捶一捶过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取时间" prop="receiveTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.receiveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择领取时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBeat, getBeat, delBeat, addBeat, updateBeat, exportBeat } from "@/api/system/beat";

export default {
  name: "Beat",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户捶一捶记录表格数据
      beatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        ordersId: undefined,
        goodsId: undefined,
        goodsName: undefined,
        goodsImg: undefined,
        beatSum: undefined,
        beatInto: undefined,
        receive: undefined,
        beatOutTime: undefined,
        receiveTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户捶一捶记录列表 */
    getList() {
      this.loading = true;
      listBeat(this.queryParams).then(response => {
        this.beatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        ordersId: undefined,
        goodsId: undefined,
        goodsName: undefined,
        goodsImg: undefined,
        beatSum: undefined,
        beatInto: undefined,
        receive: undefined,
        createTime: undefined,
        updateTime: undefined,
        beatOutTime: undefined,
        receiveTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户捶一捶记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBeat(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户捶一捶记录";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateBeat(this.form).then(response => {
               this.msgSuccess("修改成功");
               this.open = false;
               this.getList();
            });
          } else {
            addBeat(this.form).then(response => {
               this.msgSuccess("新增成功");
               this.open = false;
               this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户捶一捶记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBeat(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户捶一捶记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBeat(queryParams);
        }).then(response => {
          this.download(response.data);
        }).catch(function() {});
    }
  }
};
</script>
